#include "voldet.h"
#include "adc_hw.h"
#include "algorithm.h"
#include "tim_hw.h"
#include "sleep.h"
#include "peripheral.h"
#include "CANInit.h"
#include "L610.h"
#include "debug_prt.h"

ADC_DataTypeDef					tagADC = {0};

void vol_detect(void)
{
	uint8_t i = 0;
	//多次采样，使用电压平均值
	switch (tagADC.sta)
	{
	case 0:
		tagADC.sta = 1;
		tagADC.cnt = 0;
		tagADC.sum = getAdcVolt(0);
		tagADC.avg = tagADC.sum;
		break;

	case 1:
		tagADC.sta = 2;
		tagADC.adc_dly_ms_old = get1msCounter();
	  break;
	case 2:
		if( sleepStatue == 0x03 )
		{
			if (tagADC.cnt < 20)	//采集100次，之后求平均
			{
				tagADC.cnt++;
				tagADC.sum += getAdcVolt(0);
				tagADC.sta = 1;
			}
			else
			{
				tagADC.sta++;
				tagADC.adc_dly_ms_old = 0;
				tagADC.adc_dly_ms = 0;
			}
		}
		else
		{
			tagADC.adc_dly_ms = get1msCounter();
			if(getTim1MsElaps(tagADC.adc_dly_ms , tagADC.adc_dly_ms_old) > _MS(40))
			{
				if (tagADC.cnt < 20)	//采集100次，之后求平均
				{
					tagADC.cnt++;
					tagADC.sum += getAdcVolt(0);
					tagADC.sta = 1;
				}
				else
				{
					tagADC.sta++;
					tagADC.adc_dly_ms_old = 0;
					tagADC.adc_dly_ms = 0;
				}
			}
		}
		break;

	case 3:
		tagADC.sum += getAdcVolt(0);
		tagADC.sum -= tagADC.avg;
		if (tagADC.cnt != 0)
		{
			tagADC.avg = tagADC.sum / tagADC.cnt;
			tagADC.cnt = 0;
			/*加入缓存BAT采用缓存，进行电池稳定检测 - YXX 2022.04.21****/
			if( tagADC.pos < BAT_VALUE_SMP_NUM )
			{
				tagADC.val[tagADC.pos] = tagADC.avg;
				tagADC.pos++;
			}
			else
			{
				tagADC.pos = BAT_VALUE_SMP_NUM;
				for( i = 0 ; i < ( BAT_VALUE_SMP_NUM -1 ); i ++ )
				{
					tagADC.val[i] = tagADC.val[i+1];
				}
				tagADC.val[BAT_VALUE_SMP_NUM -1] = tagADC.avg;
			}
			/********************************************************/
		}
		else
		{
			tagADC.sta = 0;
		}
		break;

	default:
		tagADC.sta = 0;
		break;
	}
}

u32 u32AutoBATVol(void)//电池电压
{
	return (u32)(tagADC.avg);
}


u32 u32AutoBatVal( void ) //电池稳定点数据采集
{
	u32 val = 0;
	
	if( tagADC.pos == BAT_VALUE_SMP_NUM )
	{
		if( tagADC.val[BAT_VALUE_SMP_NUM -1] > tagADC.val[0] )
		{
			val = tagADC.val[BAT_VALUE_SMP_NUM -1] - tagADC.val[0];
		}
	}
	return val;
}


/***************************************************************************************
判断车辆小电瓶电压段时间内出现波动，CAN线上没有数据变化则任务系统异常，需要重新进行复位
***************************************************************************************/
void Vehicle_BatVol_Check( u8 *st )
{
	#define VEH_BAT_VOL_LEVEL ( 500 )
	static uint8_t can_recv_cnt = 0;
	static uint8_t bat_vol_cmp = 0;
	static uint16_t can_dly_new_ms = 0;
	static uint16_t can_dly_old_ms = 0;
	
	if( st == NULL )
	{
		return;
	}
	if(( u32AutoBatVal() > VEH_BAT_VOL_LEVEL ) && ( bat_vol_cmp == 0 ))
	{
		*st = 0;
		bat_vol_cmp = 1;
	}

	switch ( bat_vol_cmp )
	{
		case 1:
			bat_vol_cmp = 2;
			can_dly_old_ms = get1msCounter();
			break;
		case 2:
			if( sleepStatue == 0x03 )
			{
				//延迟400ms，单位片轮时间 40ms
				if( ( can_recv_cnt ++ ) >= 10 )
				{
					can_recv_cnt = 0;
					if( *st != 1 )
					{
						bat_vol_cmp = 3;
					}
					else
					{
						bat_vol_cmp = 0;
					}
				}
			}
			else
			{
				can_dly_new_ms = get1msCounter();
				if(getTim1MsElaps(can_dly_new_ms , can_dly_old_ms ) > _MS(400))
				{
					if( *st != 1 )
					{
						bat_vol_cmp = 3;
					}
					else
					{
						bat_vol_cmp = 0;
					}
				}
			}
			break;
		case 3:
			reBootMe();		/*复位系统*/
			break;
		default:
			break;
	}
}

/***************************************************************************************
判断当系统电源电压大于13V时，CAN总线没有数据需要重新对CAN芯片电源进行复位操作
***************************************************************************************/
void Vehicle_BatVol_CanPowerCtr( u8 *st )
{
	#define VEH_BAT_VOL_CAN_STOP_LEVEL ( 13000 )
	static uint8_t bat_vol_st = 0;
	static uint16_t bat_vol_new_ms = 0;
	static uint16_t bat_vol_old_ms = 0;
	static uint16_t bat_vol_cnt = 0;

	if(( u32AutoBATVol() >= VEH_BAT_VOL_CAN_STOP_LEVEL ) && ( bat_vol_st == 0 ))
	{
		bat_vol_st = 1;
	}
	
	switch ( bat_vol_st )
	{
		case 1:
			bat_vol_st = 2;
			bat_vol_old_ms = get1msCounter();
			break;
		case 2:
			if( sleepStatue == 0x03 )
			{
				if( *st != 0 )	/*检测CAN 中断数据是否有效，如果有效重新启动检测*/
				{
					*st = 0;
					bat_vol_st = 0;
				}
				else
				{
					//延迟30000ms，单位片轮时间 40ms
					if( ( bat_vol_cnt ++ ) >= 750  )
					{
						bat_vol_cnt = 0;
						bat_vol_st = 3;
					}
				}
			}
			else
			{
				if( *st != 0 )	/*检测CAN 中断数据是否有效，如果有效重新启动检测*/
				{
					*st = 0;
					bat_vol_st = 0;
				}
				else
				{
					bat_vol_new_ms = get1msCounter();
					if(getTim1MsElaps(bat_vol_new_ms , bat_vol_old_ms ) > _MS( 40 ))
					{
						bat_vol_old_ms = get1msCounter();
						if( ( bat_vol_cnt ++ ) >= 750  )
						{
							bat_vol_cnt = 0;
							bat_vol_st = 3;
						}
					}
				}
			}
			break;
		case 3:
			bat_vol_st = 0;
			rebootCANHardware();	//启动CAN上电复位机制
			break;
		default:
			break;
	}
}


